package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils.NetType;
import com.hundsun.yr.universal.library.utils.HsStringUtils;
import com.hundsun.yr.universal.library.utils.RegularUtils;
import com.hundsun.yr.universal.library.view.hsdialog.HSDialog.HSDialogOnDismissListener;
import com.hundsun.yr.universal.library.view.hsdialog.HSDialog.HSDialogOnItemClickListener;
import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.yr.universal.library.widget.HSEditText;
import com.hundsun.yr.universal.library.widget.HSPassedwordEditText;
import com.hundsun.yr.universal.p2p.datahandler.UpdatePhoneHandler;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.widget.ValidateButton;

/**
 * 修改手机号
 *
 * @author tonglj
 */
public class SetUpdatePhoneActivity extends BaseActivity implements
        OnClickListener, HSDialogOnDismissListener, HSDialogOnItemClickListener {
    private final String TAG = "SetUpdatePhoneActivity";
    private ValidateButton btn_getvalidate;
    private HSButton btn_submit;

    /**
     * 登录密码
     */
    private HSPassedwordEditText et_updatephone_password;

    /**
     * 新手机号码
     */
    private HSEditText et_updatephone_newphonenum;

    /**
     * 验证码
     */
    private HSEditText et_updatephone_checknum;

    private String password;

    private String newPhoneNum;

    private String checkNum;

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return true;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_set_update_phone;
    }

    @Override
    protected void onEveComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void initViewsAndEvents() {
        findView();
        setToolbarTitle(R.string.update_phone);
        // 初始化获取验证码按钮
        btn_getvalidate.init(false, TAG);

        btn_getvalidate.setOnClickListener(this);
        btn_submit.setOnClickListener(this);

        et_updatephone_newphonenum.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                String newPhoneNumTmp = s.toString().replaceAll(" ", "");
                // TODO Auto-generated method stub
                if (HsStringUtils.isBlank(newPhoneNum))
                    return;
                if (newPhoneNumTmp.length() != 11)
                    return;
                if (!newPhoneNum.equals(newPhoneNumTmp))
                    btn_getvalidate.reset();
            }
        });

        mDataHandler = new UpdatePhoneHandler(mContext);

    }


    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }

    private void findView() {
        btn_getvalidate = findView(R.id.btn_updatephone_validate);
        btn_submit = findView(R.id.btn_updatephone_submit);
        et_updatephone_password = findView(R.id.et_updatephone_password);
        et_updatephone_newphonenum = findView(R.id.et_updatephone_newphonenum);
        et_updatephone_checknum = findView(R.id.et_updatephone_checknum);
    }

    @Override
    protected void onNetworkConnected(NetType type) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onNetworkDisConnected() {
        // TODO Auto-generated method stub

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void onDestroy() {
        btn_getvalidate.onDestroy();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_updatephone_validate:
                newPhoneNum = CommTool.getValue(et_updatephone_newphonenum);
                if (RegularUtils.isPhoneNum(newPhoneNum)) {
                    mDataHandler.interactiveSilent(mDataHandler,
                            R.string.method_sendSms, newPhoneNum);
                    btn_getvalidate.setCountdown();
                } else {
                    showToast(R.string.updatephone_phone_rightcheck);
                }
                // TODO 处理发送验证码请求
                break;
            case R.id.btn_updatephone_submit:
                if (checkPass()) {
                    btn_getvalidate.reset();
                    mDataHandler.interactive(mDataHandler,
                            R.string.method_updatePhone, newPhoneNum, password,
                            checkNum);
                }
                break;
            default:
                break;
        }

    }

    /**
     * 输入框校验
     *
     * @return
     */
    private boolean checkPass() {
        checkNum = CommTool.getValue(et_updatephone_checknum);
        password = CommTool.getValue(et_updatephone_password);
        newPhoneNum = CommTool.getValue(et_updatephone_newphonenum);
        if (TextUtils.isEmpty(checkNum)) {
            showToast(R.string.validate_emptycheck);
        } else if (TextUtils.isEmpty(password)) {
            showToast(R.string.register_pass_emptycheck);
        } else if (checkNum.length() < 4) {
            showToast(R.string.register_pass_emptycheck);
        } else if (!RegularUtils.regular(RegularUtils.REGEX_PWD, password)) {
            showToast(R.string.register_pass_rightcheck);
        } else if (RegularUtils.isPhoneNum(newPhoneNum)) {
            return true;
        } else {
            showToast(R.string.updatephone_phone_rightcheck);
        }

        return false;
    }

    @Override
    public void onDismiss(Object o) {

    }


    @Override
    public void onItemClick(Object o, int position) {
        if (position == 0) {
//            readyGo(BindNameActivity.class);
        }
    }
}
